﻿
using System.Speech.Synthesis;

namespace Liju.Common
{
    /// <summary>
    /// 声音播放控件
    /// </summary>
    public class VoiceControl
    {
        private SpeechSynthesizer synthesizer;
        private bool isPlaying;

        public VoiceControl()
        {
            synthesizer = new SpeechSynthesizer();
            isPlaying = false;
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="text">播放内容</param>
        public void Speak(string text)
        {
            if (isPlaying)
            {
                synthesizer.SpeakAsyncCancelAll();//停止当前播放的语音
            }
            isPlaying = true;
            synthesizer.SpeakAsync(text); //异步播放新的语音
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            if (isPlaying)
            {
                synthesizer.SpeakAsyncCancelAll();
                isPlaying = false;
            }
        }
    }
}
